package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;
    @Override
    public Map<String, Object> customerList(Integer page, Integer rows, String customerName) {
        List<Customer> customerList = customerDao.getCustomerList((page-1)*rows, rows,customerName);
        Long total = customerDao.count();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", customerList);
        return map;

    }

    @Override
    public ServiceVO savaCustomer(Customer customer) {
        if (customer.getCustomerId() != null) {
            //修改
            customerDao.updateCustomer(customer.getCustomerId(),customer.getCustomerName(),customer.getContacts(),customer.getPhoneNumber(),customer.getAddress(),customer.getRemarks());
        }else {
            //添加
            customerDao.insertCustomer(customer.getCustomerName(),customer.getContacts(),customer.getPhoneNumber(),customer.getAddress(),customer.getRemarks());
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS,null);
    }

    @Override
    public void deleteById(String ids) {
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        customerDao.deleteById(list);
    }
}
